<?php
/**
 * @copyright	Copyright (C) 2010 Michael Richey. All rights reserved.
 * @license		GNU General Public License version 3; see LICENSE.txt
 */
// no direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
jimport('joomla.language.language');
jimport('joomla.language.helper');
class plgSystemAutoCommunity extends JPlugin
{
	function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
	}
	function onAfterRoute()
	{		
		global $mainframe;
		$cron = JRequest::getVar('cron','');
		$redirect = JRequest::getVar('autoredirect','');
		$app = &JFactory::getApplication();
		if($redirect)
		{
			include_once(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');				
			$db = & JFactory::getDBO();
			$db->setQuery("SELECT a.id,a.alias,a.catid,a.sectionid,b.alias as calias  FROM #__content a INNER JOIN #__categories b ON a.catid = b.id WHERE a.state=1  ORDER BY a.id DESC",0,1);
			$article = $db->loadObject();
			
			$url = JRoute::_(ContentHelperRoute::getArticleRoute("$article->id:$article->alias","$article->catid:$article->calias",$article->sectionid));
			//echo JUtility::dump($url);die;
			$app->redirect($url);
			$app->close();
		}
		if(!$cron)
			return;
		
		
		if ($app->isSite()) {
			include_once JPATH_BASE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'query.php';			
			include_once JPATH_BASE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php';			
			include_once JPATH_BASE.DS.'components'.DS.'com_content'.DS.'models'.DS.'frontpage.php';
			include_once dirname(__FILE__).DS.'autocommunity'.DS.'twitter.class.php';
			//$filetime =filemtime(file_get_contents(dirname(__FILE__).DS.'autocommunity'.DS.'param.ini'));
			$pitem =intval(file_get_contents(dirname(__FILE__).DS.'autocommunity'.DS.'param.ini'));	
			
			$sleep 				= $this->params->get('sleep',15);
			$filetime = $sleep*50 +$filetime;
			/*$now = time();
			if($now<$filetime)
				return;*/
			$consumerKey 				= $this->params->get('consumerKey');
			$consumerSecret				= $this->params->get('consumerSecret');
			$accessToken				= $this->params->get('accessToken');
			$accessTokenSecret 			= $this->params->get('accessTokenSecret');
			
			
			$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
			//if (!$twitter->authenticate()) {
			if(1!=1){
				echo('Invalid name or password');
			}
			else{
				//processing only when twitter is ok.
				//echo 'OK';die;
				$db = &JFactory::getDBO();
				$frontpage = new ContentModelFrontpage();
				$query = $frontpage->_buildQuery();	
				$query = str_replace('ORDER BY','ORDER BY a.id DESC, ',$query);	
				$query = str_replace('WHERE 1','WHERE a.id >'.$db->Quote($pitem),$query);	
					
				//echo JUtility::dump($query);die;
				$db->setQuery($query,0,50);
				$items = $db->loadObjectList();
				if($items[0]->id<$pitem)
					$twitter->authenticate();
				$newItem = $pitem;				
				foreach($items as $item)
				{
					if($item->id<$pitem)
						break;
					usleep($sleep);
					$url = JURI::root().JRoute::_(ContentHelperRoute::getArticleRoute($item->slug,$item->catslug,$item->sectionid));					
					$status = $item->title.' '.$url;					
					try{
						$res = $twitter->request('statuses/update', array('status' => $status,'wrap_links'=>true));
					}
					catch(exception $e){
						echo $e->getMessage();
						continue;
					}				
								
				}
				if($items[0]->id>$pitem)
					file_put_contents(dirname(__FILE__).DS.'autocommunity'.DS.'param.ini',$items[0]->id);
				$this->params->set('processed_item',$newItem);	
							
			}
			
			
		}
	}
}
